package com.bayss.web.controller;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.classify.Classify;
import com.bayss.core.entity.goods.ListClassify;
import com.bayss.core.entity.goods.Sku;
import com.bayss.core.model.SpuSkuInfo;
import com.bayss.core.service.GoodsService;
import com.bayss.core.service.sku.SkuTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品
 *
 * @author helen
 */
@RestController
public class GoodsController extends ApiBaseController {
    @Autowired
    private SkuTagService skuTagService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 微信用户获取分类
     *
     * @return success
     */
    @RequestMapping("/goods/list/classify.json")
    public ResultMsg list() {
        List<ListClassify> listClassify = goodsService.findListClassify();
        return ResultMsg.success(listClassify);
    }


    /**
     * 微信用户获取分类中的商品
     * ClassifyService
     *
     * @return success
     */
    @RequestMapping("/goods/list.json")
    public ResultMsg goodsList(@RequestParam(name = "classifyCode") Long classifyCode) {
        List<SpuSkuInfo> goodsByClassifyCode = goodsService.findGoodsByClassifyCode(classifyCode);
        return ResultMsg.success(goodsByClassifyCode);
    }


    /**
     * 微信用户获取sku详细信息
     *
     * @return success
     */
    @RequestMapping("/goods/desc.json")
    public ResultMsg goodsDesc(@RequestParam(name = "skuCode") String skuCode) {
        Sku skuDesc = null;
        if (skuCode.startsWith("self_sku_")) {
            skuDesc = goodsService.getSelfSkuDesc(skuCode);
        } else {
            skuDesc = goodsService.getSkuDesc(skuCode);
        }
        return ResultMsg.success(skuDesc);
    }

    /**
     *微信用户获取sku详细信息(批量）
     * @return
     */
    @RequestMapping("/goods/goodsDescBatch.json")
    public ResultMsg goodsDescBatch(@RequestParam(name = "skuCodes") String skuCodes) {
        List<Sku> skuList = new ArrayList<>();
        for (String skuCode : skuCodes.split(",")) {
            if (skuCode.startsWith("self_sku_")) {
                skuList.add(goodsService.getSelfSkuDesc(skuCode));
            } else {
                skuList.add(goodsService.getSkuDesc(skuCode));
            }
        }
        return ResultMsg.success(skuList);
    }

    /**
     * 微信用户获取sku详细信息
     *
     * @return success
     */
    @RequestMapping("/goods/ingredient.json")
    public ResultMsg goodsIngredient() {
        List<Classify> ingredient = goodsService.getIngredient();
        return ResultMsg.success(ingredient);
    }

    /**
     * 自选餐所需要的规则
     *
     * @return
     */
    @RequestMapping("/goods/self/rule.json")
    public ResultMsg goodsSelfRule() {
        Map<String, String> selfSkuRule = goodsService.getSelfSkuRule();
        return ResultMsg.success(selfSkuRule);
    }

    /**
     * skuTag查询全部
     * @return
     */
    @GetMapping(value = "/goods/skuTag/list.json")
    public ResultMsg skuTagList() {
        try {
            return ResultMsg.success(skuTagService.list());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "查询失败");
        }
    }
}
